package servlet;

import bean.BookBean;
import service.IBookService;
import service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Set;

@WebServlet(name = "ShowResult",value = "/showResult")
public class ShowResult extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");


        IBookService bookService = new BookServiceImpl();

        HttpSession session = request.getSession();
        //取出购物车
        Map<Integer,Integer> shopCar = (Map<Integer, Integer>) session.getAttribute("shopCar");
       //显示结果
         Set<Integer> keys =  shopCar.keySet();
        PrintWriter out = response.getWriter();

        out.print("<hr/>");
        out.print("<html>");
        out.print("<body>");
        out.print("<a href='show'>返回继续购买</a>");
        out.print("<table border='1'>");
        out.print("<tr><td>图片</td><td>书名</td><td>价格</td><td>购买数量</td><td>总计</td></tr>");
        for (Integer key : keys) {
            BookBean bookBean =bookService.showBookDetailInfo(key);
            out.print("<tr><td><img style='width:50px;height:80px' src='javaImg/"+bookBean.getPic()+"'/></td><td>"+bookBean.getName()+"</td><td>"+bookBean.getPrice()+"</td><td>"+shopCar.get(key)+"</td><td>"+(bookBean.getPrice()*shopCar.get(key))+"</td></tr>");
        }
        out.print("</table>");
        out.print("</body>");
        out.print("</html>");



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request,response);
    }
}
